<template>
  <div class="m-25 mt-65">
    <div class="normal-box-mod">
      <div class="normal-box-hd clearfix">
        <div class="normal-box-title">
          <div class="decorate-square"></div>
          <h2>消息通知</h2>
        </div>
      </div>
      <div class="normal-box-bd">
        <Card style="margin-bottom: 10px">
          <div class="normal-list-page-hd clearfix">
            <Select v-model="queryData.recId"
                    :clearable="true"
                    style="width: 200px">
              <Option v-for="item in modal.recIdList"
                      :value="item.id"
                      :key="item.id">{{ item.name }}</Option>
            </Select>
            <Button type="primary"
                    custom-icon="iconfont icon-chaxun"
                    size="small"
                    @click="inQuire">查询</Button>
          </div>
        </Card>
        <div class="normal-list-page-mod">
          <div class="mb-2">
            <Button-group>
              <Button
                custom-icon="iconfont icon-fangkuai2"
                size="small"
                type="primary"
                @click="showCreateModal"
              >
                新建
              </Button>
            </Button-group>
          </div>
          <div class="normal-list-page-bd">
            <Table
              border
              :loading="list.loading"
              :columns="list.columns"
              :data="list.tableData"
              @on-selection-change="selectionChange"
            >
                 <template slot-scope="{ row }"
                        slot="desc">
                 <Button type="primary" size="small" @click="openDesc(row.content)">查看</Button>
              </template>
            </Table>
          </div>
          <div class="normal-list-page-ft clearfix">
            <Page
              class="fr"
              :total="queryData.totalRecord"
              :page-size="queryData.pageSize"
              :current="queryData.page"
              @on-change="changePage"
              @on-page-size-change="onPageSizeChange"
              show-elevator
              show-total
              show-sizer
            ></Page>
          </div>
        </div>
        <Modal v-model="modal.mallAdvertising.bigPicVisible" width="1000px">
          <img
            :src="modal.mallAdvertising.bigPicUrl"
            style="width: 50%; height: 50%; display: block; margin: 0 auto"
          />
          <div slot="footer" />
        </Modal>
        <!--新建&修改-->
        <Modal
          v-model="modal.mallAdvertising.isShow"
          :loading="modal.mallAdvertising.loading"
          :mask-closable="false"
          :title="modal.mallAdvertising.title"
          @on-ok="postData"
          width="800px"
        >
          <Form
            ref="systemFrom"
            :model="modal.mallAdvertising.formData"
            :rules="modal.mallAdvertising.rules"
            :label-width="120"
          >
            <FormItem label="接受者" prop="recId" >
            <Select v-model="modal.mallAdvertising.formData.recId" class="mr-10" :clearable=flog style="width:100px" >
              <Option v-for="item in modal.recIdList" :value="item.id"  :key="item.id">{{item.name}}</Option>
            </Select>
          </FormItem>
            <FormItem label="标题" prop="title">
              <Input
                placeholder="请输入标题"
                v-model="modal.mallAdvertising.formData.title"
                style="width: 300px"
              />
            </FormItem>
            <FormItem label="内容" prop="content">
                  <wang-editor v-if="modal.mallAdvertising.isShow"
                           :value="modal.mallAdvertising.formData.content"
                           @change="(v)=>{modal.mallAdvertising.formData.content=v}"></wang-editor>

            </FormItem>

            <FormItem label="上传图片" prop="imgUrl">
              <upload-image
                v-if="modal.mallAdvertising.isShow"
                ref="upload"
                :uploadList.sync="modal.mallAdvertising.formData.uploadList"
                @imageupload="calcImg"
                imgSize="1"
              ></upload-image>
            </FormItem>
          </Form>
        </Modal>
         <!-- 查看详情 -->
        <Modal v-model="showDesc"
               title="详情"
               width="600px">
           <div style="max-width:100%" v-html="descHtml"></div>
        </Modal>
      </div>
    </div>
  </div>
</template>
<script>
import notifyApi from '@/api/zhcg/notify.js'
import { Message } from 'iview'
import Uploadoss from '@/components/uploadoss'
import uploadImage from '@/components/upload-image/upload-image'
import WangEditor from '@/components/Editor/WangEditor.vue'
export default {
  components: {
    uploadImage,
    Uploadoss,
    WangEditor
  },
  name: 'user',
  data () {
    return {
      // 列表
      modal: {
        recIdList: [
          { id: 1, name: '用户' },
          { id: 2, name: '回收员' },
          { id: 3, name: '清运员' },
          { id: 4, name: '政府管理员' },
          { id: 5, name: '物业管理员' },
          { id: 6, name: '督导员' }
        ],
        mallAdvertising: {
          isShow: false,
          bigPicVisible: false,
          loading: true,
          bigPicUrl: '',
          type: 'create', // create 新建，update 修改
          title: '新建',
          rules: {
            title: [
              {
                required: true,
                message: '请填写标题',
                trigger: 'blur'
              }
            ],
            content: [
              {
                required: true,
                message: '请填写内容',
                trigger: 'blur'
              }
            ],
            recId: [
            {
              type: 'number',
              required: true,
              message: '请选择接收者',
              trigger: 'change'
            }
            ],
            imgUrl: [
              { required: true, message: '请上传图片', trigger: 'change' }
            ]
          },
          formData: {}
        }
      },
      bigPicVisible: false,
      bigPicUrl: '',
      list: {
        loading: false,
        selections: [],
        // 列表展示数据
        tableData: [],
        // 表格表头
        columns: [
          // {
          //   type: 'selection',
          //   width: 50,
          //   align: 'center'
          // },
          /** 开始自动生成 */
          { title: 'id', key: 'id', align: 'center', width: '100px' },
          { title: '发送者id', key: 'sendId', align: 'center', width: '100px' },
          { title: '发送者', key: 'sendName', align: 'center', width: '100px' },
          { title: '标题', key: 'title', minWidth: 150, align: 'center' },
          { title: '内容', key: 'content', minWidth: 200, align: 'center', slot: 'desc' },
          {
            title: '图片',
            key: 'imgUrl',
            align: 'center',
            minWidth: 100,
            render: (h, params) => {
              return h('img', {
                attrs: {
                  src: params.row.imgUrl
                },
                style: {
                  width: '100%'
                  //   height: "35px",
                },
                on: {
                  click: () => {
                    this.showBigPic(params.row.imgUrl) // 此处再用一个方法，建一个modal把图放大。
                  }
                }
              })
            }
          },
          {
            title: '消息类型',
            key: 'type',
            align: 'center',
            minWidth: 100,
            render: (h, params) => {
              if (params.row.type === 1) {
                return h('div', [
                  h(
                    'span',
                    {
                      style: {
                        display: 'inline-block',
                        width: '100%',
                        overflow: 'hidden',
                        textOverflow: 'ellipsis',
                        whiteSpace: 'nowrap',
                        color: '#5ed71a'
                      },
                      domProps: {
                        title: '政府通知'
                      }
                    },
                    '政府通知'
                  )
                ])
              } else if (params.row.type === 2) {
                return h('div', [
                  h(
                    'span',
                    {
                      style: {
                        display: 'inline-block',
                        width: '100%',
                        overflow: 'hidden',
                        textOverflow: 'ellipsis',
                        whiteSpace: 'nowrap',
                        color: '#d72c23'
                      },
                      domProps: {
                        title: ' 警告和处罚通知'
                      }
                    },
                    ' 警告和处罚通知'
                  )
                ])
              } else if (params.row.type === 3) {
                return h('div', [
                  h(
                    'span',
                    {
                      style: {
                        display: 'inline-block',
                        width: '100%',
                        overflow: 'hidden',
                        textOverflow: 'ellipsis',
                        whiteSpace: 'nowrap',
                        color: '#2a3ad7'
                      },
                      domProps: {
                        title: ' 小区物业'
                      }
                    },
                    ' 小区物业'
                  )
                ])
              } else if (params.row.type === 4) {
                return h('div', [
                  h(
                    'span',
                    {
                      style: {
                        display: 'inline-block',
                        width: '100%',
                        overflow: 'hidden',
                        textOverflow: 'ellipsis',
                        whiteSpace: 'nowrap',
                        color: '#2a3ad7'
                      },
                      domProps: {
                        title: ' 平台'
                      }
                    },
                    ' 平台'
                  )
                ])
              }
            }
          },
          {
            title: '接收者',
            key: 'recId',
            align: 'center',
            minWidth: 100,
            render: (h, params) => {
              if (params.row.recId === 1) {
                return h('div', [
                  h(
                    'span',
                    {
                      style: {
                        display: 'inline-block',
                        width: '100%',
                        overflow: 'hidden',
                        textOverflow: 'ellipsis',
                        whiteSpace: 'nowrap',
                        color: '#5ed71a'
                      },
                      domProps: {
                        title: '用户'
                      }
                    },
                    '用户'
                  )
                ])
              } else if (params.row.recId === 2) {
                return h('div', [
                  h(
                    'span',
                    {
                      style: {
                        display: 'inline-block',
                        width: '100%',
                        overflow: 'hidden',
                        textOverflow: 'ellipsis',
                        whiteSpace: 'nowrap',
                        color: '#d72c23'
                      },
                      domProps: {
                        title: ' 回收员'
                      }
                    },
                    ' 回收员'
                  )
                ])
              } else if (params.row.recId === 3) {
                return h('div', [
                  h(
                    'span',
                    {
                      style: {
                        display: 'inline-block',
                        width: '100%',
                        overflow: 'hidden',
                        textOverflow: 'ellipsis',
                        whiteSpace: 'nowrap',
                        color: '#2a3ad7'
                      },
                      domProps: {
                        title: ' 清运员'
                      }
                    },
                    ' 清运员'
                  )
                ])
              }
            }
          },
          {
            title: '发送时间',
            key: 'createTime',
            align: 'center',
            width: '200px'
          },
          /** 自动生成结束 */
          {
            title: '操作',
            align: 'center',
            minWidth: 100,
            fixed: 'right',
            render: (h, params) => {
              if (params.row.recId != 1) {
                return h('div', { class: 'action-group' }, [
                  h(
                    'Tooltip',
                    {
                      props: {
                        placement: 'left',
                        content: '编辑'
                      },
                      style: {
                        cursor: 'pointer'
                      }
                    },
                    [
                      h(
                        'Button',
                        {
                          props: {
                            type: 'primary',
                            size: 'small'
                          },
                          style: {
                            marginRight: '5px',
                            padding: '1px 2px'
                          },
                          on: {
                            click: () => {
                              this.showUpdateModal(params.row.id)
                            }
                          }
                        },
                        [
                          h('Icon', {
                            props: {
                              type: ' iconfont icon-bianjisekuai',
                              size: 18
                            }
                          })
                        ]
                      )
                    ]
                  ),
                  h(
                    'Tooltip',
                    {
                      props: {
                        placement: 'right',
                        content: '删除'
                      },
                      style: {
                        cursor: 'pointer'
                      }
                    },
                    [
                      h(
                        'Button',
                        {
                          props: {
                            type: 'error',
                            size: 'small'
                          },
                          style: {
                            marginRight: '5px',
                            padding: '1px 2px'
                          },
                          on: {
                            click: () => {
                              this.delData([params.row.id])
                            }
                          }
                        },
                        [
                          h('Icon', {
                            props: {
                              type: ' iconfont icon-shanchusekuai',
                              size: 18
                            }
                          })
                        ]
                      )
                    ]
                  )
                ])
              }
            }
          }
        ]
      },
      // 页面查询数据
      queryData: {
        limit: 10,
        page: 1,
        totalRecord: 0,
        name: ''
      },
      showDesc: false,
      descHtml: ''
    }
  },
  methods: {
    // 图片的校验
    calcImg () {
      let imgUrl = this.modal.mallAdvertising.formData.uploadList.map((v) => {
        if (v.length < 1) return v
        v = v.response.res.requestUrls[0]
        return v
      })
      this.modal.mallAdvertising.formData.imgUrl = imgUrl[0]
    },
    // 显示新建弹窗
    showCreateModal () {
      this.modal.mallAdvertising.type = 'create'
      this.modal.mallAdvertising.title = '新建'
      this.modal.mallAdvertising.formData = {}
      this.modal.mallAdvertising.isShow = true
    },

    showBigPic (item) {
      this.modal.mallAdvertising.bigPicVisible = true
      this.modal.mallAdvertising.bigPicUrl = item
    },
    // 获取列表
    getTableData () {
      const that = this
      that.queryData.totalRecord = undefined
      notifyApi
        .getPageList(that.queryData)
        .then((res) => {
          that.list.tableData = res.data.list
          that.queryData.totalRecord = res.data.totalRecord
          that.queryData.page = res.data.pageNum
          that.queryData.limit = res.data.pageSize
        })
        .catch((error) => {
          Message.error(error)
        })
    },
    postData () {
      const that = this
      that.$refs['systemFrom'].validate((valid) => {
        if (valid) {
          if (!that.modal.mallAdvertising.formData.id) {
            // 新建
            notifyApi
              .add(that.modal.mallAdvertising.formData)
              .then((res) => {
                that.modal.mallAdvertising.loading = false
                if (!res.code) {
                  that.modal.mallAdvertising.isShow = false
                  that.getTableData()
                  that.$Notice.success({
                    title: '保存成功!'
                  })
                } else {
                  that.modal.mallAdvertising.isShow = true
                  that.$Notice.error({
                    title: res.message
                  })
                }
              })
              .catch((error) => {
                Message.error(error)
              })
          } else {
            notifyApi.update(
              that.modal.mallAdvertising.formData.id,
              that.modal.mallAdvertising.formData
            )
              .then((res) => {
                that.modal.mallAdvertising.loading = false
                console.log(that.modal.mallAdvertising.formData)
                if (!res.code) {
                  that.modal.mallAdvertising.isShow = false
                  that.getTableData()
                  that.$Notice.success({
                    title: '保存成功!'
                  })
                } else {
                  that.modal.mallAdvertising.isShow = true
                  that.$Notice.error({
                    title: res.message
                  })
                }
              })
              .catch((error) => {
                Message.error(error)
              })
          }
        } else {
          that.modal.mallAdvertising.loading = false
          that.$nextTick(() => {
            that.modal.mallAdvertising.loading = true
          })
        }
      })
    },

    // 删除
    delData (ids) {
      if (!ids.length) {
        return
      }
      this.$Modal.confirm({
        title: '确认删除',
        content:
          '<p>您确认删除id为 <strong>' +
          ids.toString() +
          '</strong> 的记录吗?</p><p>删除后将无法撤销，请谨慎操作！</p>',
        loading: true,
        onOk: () => {
          notifyApi.del({
            ids: ids
          }).then((res) => {
            this.$Modal.remove()
            if (!res.code) {
              this.$Notice.success({
                title: '删除成功'
              })
              this.queryData.page = 1
              this.getTableData()
            } else {
              this.$Notice.error({
                title: res.message
              })
            }
          })
        }
      })
    },
    showUpdateModal (id) {
      this.$refs.systemFrom.resetFields()
      this.modal.mallAdvertising.type = 'update'
      this.modal.mallAdvertising.title = '修改'
      // 不请求后端，直接list取对象
      // this.modal.mallAdvertising.formData = JSON.parse(
      //   JSON.stringify(this.list.tableData)
      // )[index];
      const that = this
      notifyApi.getDetail(id).then(res => {
        that.modal.mallAdvertising.formData = res.data
        let item = {
          status: 'finished',
          response: {
            res: {
              requestUrls: []
            }
          }
        }
        if (that.modal.mallAdvertising.formData.imgUrl) {
          item.response.res.requestUrls.push(that.modal.mallAdvertising.formData.imgUrl)
          that.modal.mallAdvertising.formData.uploadList = []
          that.modal.mallAdvertising.formData.uploadList.push(item)
        } else {
          that.modal.mallAdvertising.formData.uploadList = []
        }
      })
        .catch(error => {
          Message.error(error)
        })
      this.modal.mallAdvertising.isShow = true
    },
    // 排量删除
    batchDel () {
      let ids = []
      this.list.selections.forEach((element) => {
        ids.push(element.id)
      })
      if (!ids.length) {
        this.$Message.error({
          content: '请先选择要删除的项！',
          duration: 3
        })
      }
      this.delData(ids)
    },
    // 查询
    inQuire () {
      const that = this
      that.queryData.page = 1
      that.getTableData()
    },
    // 全选，不全选
    selectionChange (args) {
      this.list.selections = args
    },

    // 每页显示记录数变更
    onPageSizeChange (pageSize) {
      this.queryData.limit = pageSize
      this.getTableData()
    },

    // 换页
    changePage (page) {
      this.queryData.page = page
      this.getTableData()
    },
    openDesc (val) {
      this.descHtml = val
      this.showDesc = true
    }
  },

  created () {
    this.getTableData()
  }
}
</script>
